package streamer_service

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/EDDYCJY/go-gin-example/models"
)

type GetStreamer struct {
	InstitutionID int
	PageNum       int
	PageSize      int
}

func (t *GetStreamer) GetStreamer() (interface{}, error) {
	maps := map[string]interface{}{
		"institution_id": t.InstitutionID,
		"is_delete":      0,
	}
	total, _ := models.GetStreamerTotal(maps)
	streamerList, _ := models.GetStreamerPage([]string{"streamer_name", "streamer_mobile", "streamer_id"}, maps, t.PageNum, t.PageSize, "")
	var lists []interface{}
	for _, v := range streamerList {

		var streamer_tag []interface{}
		for _, v1 := range v.StreamerTag {
			var tag = map[string]interface{}{
				"tag_id":   v1.TagID,
				"tag_name": v1.Tag.TagName,
			}
			streamer_tag = append(streamer_tag, tag)
		}
		params := map[string]interface{}{
			"streamer_id":     v.StreamerId,
			"streamer_mobile": v.StreamerMobile,
			"streamer_name":   v.StreamerName,
			"streamer_tag":    streamer_tag,
		}
		lists = append(lists, params)
	}
	return map[string]interface{}{
		"lists": lists,
		"total": total,
	}, nil
}

// Login 定义登录请求结构体
type AddStreamer struct {
	InstitutionID  int
	StreamerName   string
	StreamerMobile string
	TagIds         string
}

// AddGoods 执行用户登录逻辑
func (t *AddStreamer) AddStreamer() (map[string]interface{}, error) {

	// 开启事务
	tx := models.GetDB().Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"streamer_name":  t.StreamerName,
		"is_delete":      0,
	}
	streamerInfo, _ := models.GetStreamerInfo([]string{"streamer_id"}, maps)
	if streamerInfo.StreamerId != 0 {
		return nil, fmt.Errorf("该主播已存在")
	}
	maps["streamer_mobile"] = t.StreamerMobile
	StreamerId, err := models.AddStreamerDB(maps, tx)
	if err != nil {
		return nil, err
	}

	// 清理商品标签
	maps = map[string]interface{}{
		"streamer_id": StreamerId,
	}
	err = models.CleanStreamerTagAllDB(maps, tx)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	// 添加新的商品标签
	var TagList []models.StreamerTagAll
	for _, v := range strings.Split(t.TagIds, ",") {
		TagId, _ := strconv.ParseInt(v, 0, 64)
		var TagInfo = models.StreamerTagAll{
			StreamerID: StreamerId,
			TagId:      int(TagId),
		}
		TagList = append(TagList, TagInfo)
	}
	if len(TagList) > 0 {
		_, err = models.AddStreamerTagAllDB(TagList, tx)
		if err != nil {
			tx.Rollback()
			return nil, fmt.Errorf("添加主播标签失败: %v", err)
		}
	}

	// 提交事务
	err = tx.Commit().Error
	if err != nil {
		tx.Rollback()
		return nil, fmt.Errorf("提交事务失败: %v", err)
	}
	return nil, nil
}

type EditStreamer struct {
	InstitutionID  int
	StreamerID     int
	StreamerName   string
	StreamerMobile string
	TagIds         string
}

// AddGoods 执行用户登录逻辑
func (t *EditStreamer) EditStreamer() (map[string]interface{}, error) {

	// 开启事务
	tx := models.GetDB().Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	var maps = map[string]interface{}{
		"streamer_id":    t.StreamerID,
		"institution_id": t.InstitutionID,
	}
	var update = map[string]interface{}{
		"streamer_name":   t.StreamerName,
		"streamer_mobile": t.StreamerMobile,
	}
	err := models.EditStreamerDB(maps, update, tx)
	if err != nil {
		return nil, err
	}

	// 清理商品标签
	maps = map[string]interface{}{
		"streamer_id": t.StreamerID,
	}
	err = models.CleanStreamerTagAllDB(maps, tx)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	// 添加新的商品标签
	var TagList []models.StreamerTagAll
	for _, v := range strings.Split(t.TagIds, ",") {
		TagId, _ := strconv.ParseInt(v, 0, 64)
		var TagInfo = models.StreamerTagAll{
			StreamerID: t.StreamerID,
			TagId:      int(TagId),
		}
		TagList = append(TagList, TagInfo)
	}
	if len(TagList) > 0 {
		_, err = models.AddStreamerTagAllDB(TagList, tx)
		if err != nil {
			tx.Rollback()
			return nil, fmt.Errorf("添加主播标签失败: %v", err)
		}
	}

	// 提交事务
	err = tx.Commit().Error
	if err != nil {
		tx.Rollback()
		return nil, fmt.Errorf("提交事务失败: %v", err)
	}
	return nil, nil
}

type DeleteStreamer struct {
	InstitutionID int
	StreamerID    int
}

// AddGoods 执行用户登录逻辑
func (t *DeleteStreamer) DeleteStreamer() (map[string]interface{}, error) {
	var maps = map[string]interface{}{
		"streamer_id":    t.StreamerID,
		"institution_id": t.InstitutionID,
	}
	var update = map[string]interface{}{
		"is_delete": 1,
	}
	err := models.EditStreamer(maps, update)
	if err != nil {
		return nil, err
	}
	return nil, nil
}
